<?php
/* 
 * 	模型基类
 * 	作者：v.k chow
 * 	日期：2016-12-10
 *  */
class ModelBase {
	protected $db;
	protected $db_link;
	
	protected $fields = [];
	
	public $table_name;
	
	public $total_rows;
	
	public function __construct($tableName){
		require_once APP_PATH . 'core/db.php';
		$this->db = new DB();
		$this->db_link = $this->db->getLink();
		$this->table_name = $tableName;
		$desc_sql = "desc " . $this->table_name;
		$desc = $this->db->query($desc_sql);
		if($desc instanceof mysqli_result){
			while($row = $desc->fetch_assoc()){
				array_push($this->fields, $row['Field']);
			}
			$desc->close();
		}else{
			throw new Exception(mysqli_error($this->db_link));
		}
		$total_row_num_sql = "select count(*) as num from " . $this->table_name;
		$result = $this->db->query($total_row_num_sql);
		if($result instanceof mysqli_result){
			$row = ($result->fetch_assoc());
			$this->total_rows = $row['num'];
		}else{
			throw new Exception(mysqli_error($this->db_link));
		}
	}
	
	public function getAll($orderField=null, $desc=false){
		$sql = "";
		$sorttype = "";
		$desc ? $sorttype = "DESC" : $sorttype = "ASC";
		if(is_string($orderField)){
			if(in_array($this->fields, $orderField)){
				$sql = "select * from " . $this->table_name . "order by " . $orderField . " " . $sorttype;
			}else{
				throw new Exception("'".$orderField . "' field not exists!");
			}
		}else{
			$sql = "select * from " . $this->table_name;
		}
		$result = $this->db->query($sql);
		$result_arr = array();
		if($result instanceof mysqli_result){
			while($row = $result->fetch_assoc()){
				array_push($result_arr, $row);
			}
			return $result_arr;
		}else{
			return null;
		}
	}

	public function pagination($pageRowNum, $currentPage, $orderField=null, $desc=false){
		$sql="";
		if($currentPage <= 0){
			$currentPage = 1;
		}
		if($pageRowNum <= 0 || $pageRowNum > $this->total_rows){
			$pageRowNum = $this->total_rows;
		}
		$start = ($currentPage-1) * $pageRowNum;
		if(is_string($orderField)){
			if(in_array($this->fields, $orderField)){
				$sql = "select * from {$this->table_name} order by {$orderField} limit {$start},{$pageRowNum}";
			}else{
				$sql = "select * from {$this->table_name} limit {$start},{$pageRowNum}";
			}
		}else{
			$sql = "select * from {$this->table_name} limit {$start},{$pageRowNum}";
		}
		$result = $this->db->query($sql);
		$result_arr = array();
		if($result instanceof mysqli_result){
			while($row = $result->fetch_assoc()){
				array_push($result_arr, $row);
			}
			return $result_arr;
		}else{
			return null;
		}
	}
	
	public function __call($method, $args){
		$magic;
		$split = substr($method, 0, 6);
		if(strcmp(substr($method, 0, 6), "findBy") == 0){
			$magic = "findBy";
			$field = substr($method, 6, strlen($method)-6);
			if(in_array($field, $this->fields)){
				$sql = "select * from ".$this->table_name." where `{$field}`={$args[0]}";
				$result = $this->db->query($sql);
				if($result instanceof mysqli_result){
					$arr = array();
					while ($row = $result->fetch_assoc()){
						$arr[] = $row;
					}
					return $arr;
				}else{
					throw new Exception(mysqli_error($this->db_link));
				}
			}else{
				throw new Exception("method not exists!");
			}
		}
	}
}